/*
 * @Author: your name
 * @Date: 2020-10-29 15:41:49
 * @LastEditTime: 2020-11-30 17:48:18
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \profess-baidu-2\pages\home\home.js
 */

const app = getApp();
Page({
    data: {
        userInfo:app.cache.get('loginUser'),
        professRecordList: []
    },
    navigateTo() {
        if(this.juedgeUser()) return;
        swan.navigateTo({
            url: '/packageA/pages/request/request'
        });
    },
    toMySelf(){
        swan.navigateTo({
            url: '/packageC/pages/mySelf/mySelf'
        });
    },
    toUserGuide(){
        swan.navigateTo({
            url:'/packageB/pages/userGuide/userGuide'
        })
    },
    toShare() {
        if(this.juedgeUser()) return;
        swan.navigateTo({
            url:'/packageC/pages/addFriends/addFriends'
        })
    },
    toRespsone(e) {
        app.cache.put("professRecordId",e.target.dataset.id,1*60*60)
        swan.navigateTo({
            url: `/packageB/pages/response/response`
        })
    },
    toChat(e) {
         swan.navigateTo({
            url: `/packageB/pages/chat/chat?toUserId=${e.target.dataset.id}`
        });
    },
     getProfessRecordList(){
        if(this.juedgeUser()) return;

        let _this = this
        swan.request({
            url:app.globalData.URL + `/8100/profess-record/findEntityListByFromId/${this.data.userInfo.id}`,
            method: 'get',
            success:res=>{
                if(res.data.code != 200){
                    app.showToastFail(res.data.msg)
                    return;
                }
                // console.log(res.data.data);
                _this.findEntityListByToId(res.data.data, _this)
            }
        })
    },
    findEntityListByToId(list, _this){
        swan.request({
            url:app.globalData.URL + `/8100/profess-record/findEntityListByToId/${this.data.userInfo.id}`,
            method: 'get',
            success:res=>{
                if(res.data.code != 200){
                    app.showToastFail(res.data.msg)
                    return;
                }
                list.push(...res.data.data);
                console.log(list);
                _this.setData({
                    professRecordList: list
                })
            }
        })
    },
    // toProfessIdTo(){
    //     swan.navigateTo({
    //         url:'../../packageC/pages/professIdTo/professIdTo'
    //     })
    // },
    juedgeUser() {
        if(this.data.userInfo == undefined || this.data.userInfo == null  || this.data.userInfo == ''){
            this.toMySelf()
            return true;
        }
        return false;

    },
    onLoad: function () {
        // 监听页面加载的生命周期函数
//         this.juedgeUser()
    },
    onReady: function() {
        // 监听页面初次渲染完成的生命周期函数
    },
    onShow: function() {
        // 监听页面显示的生命周期函数

        let loginUser = app.cache.get('loginUser');
        console.log(loginUser);
        if(loginUser == undefined || loginUser == ''){
            return
        }

        this.setData({
            userInfo:loginUser
        })
        this.getProfessRecordList()

    },
    onHide: function() {
        // 监听页面隐藏的生命周期函数
    },
    onUnload: function() {
        // 监听页面卸载的生命周期函数
    },
    onPullDownRefresh: function() {
        // 监听用户下拉动作
    },
    onReachBottom: function() {
        // 页面上拉触底事件的处理函数
    },
    onShareAppMessage: function () {
        // 用户点击右上角转发
    }
});